package com.zhao.dota.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.dota.mapper.MatchPlayerAbilityMapper;
import com.zhao.dota.model.Ability;
import com.zhao.dota.model.MatchPlayer;
import com.zhao.dota.model.MatchPlayerAbility;
import com.zhao.dota.service.AbilityService;
import com.zhao.dota.service.MatchPlayerAbilityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MatchPlayerAbilityServiceImpl extends ServiceImpl<MatchPlayerAbilityMapper, MatchPlayerAbility> implements MatchPlayerAbilityService {

    @Resource
    private AbilityService abilityService;

    @Override
    public int saveList(Map<String, List<MatchPlayerAbility>> playerAbilityMap, Integer matchId, Map<String, MatchPlayer> playerMap) {
        int count = deleteByMatchId(matchId);
        for (String heroName : playerAbilityMap.keySet()) {
            String heroAnotherName = getHeroAnotherName(heroName);
            count += saveHeroAbility(heroName, playerAbilityMap.getOrDefault(heroName, playerAbilityMap.get(heroAnotherName)),
                    matchId, playerMap.getOrDefault(heroName, playerMap.get(heroAnotherName)));
        }
        return count;
    }

    private String getHeroAnotherName(String heroName) {
        String substring = heroName.substring("npc_dota_hero_".length());
        // vs的名字一会叫npc_dota_hero_vengefulspirit 一会叫npc_dota_hero_vengeful_spirit
        return "npc_dota_hero_" + substring.replace("_", "");
    }

    private int saveHeroAbility(String heroName, List<MatchPlayerAbility> list, Integer matchId, MatchPlayer player) {
        Map<String, Integer> abilityLevelMap = new HashMap<>();
        list.sort(Comparator.comparingInt(MatchPlayerAbility::getIndex));
        int index = 0;

        // 不知道为啥，doom的加点中会出现sven的风暴之拳（锤子），所以去掉不属于这个英雄的技能
        List<Ability> heroAbilities = abilityService.getByHeroName(heroName.replace("npc_dota_hero_", ""));
        List<String> heroAbilityNames = heroAbilities.stream().map(Ability::getName).collect(Collectors.toList());

        for (Iterator<MatchPlayerAbility> it = list.iterator(); it.hasNext(); ) {
            MatchPlayerAbility ability = it.next();
            // 不知道为啥，doom的加点中会出现sven的风暴之拳（锤子），所以去掉不属于这个英雄的技能
            if (!heroAbilityNames.contains(ability.getName())) {
                it.remove();
                continue;
            }
            Ability abi = abilityService.getByName(ability.getName());
            if (abi == null || !Boolean.TRUE.equals(abi.getShowAtLevelUp())) {
                it.remove();
                continue;
            }
            Integer level = abilityLevelMap.get(ability.getName());
            if (level != null && level >= ability.getLevel()) {
                it.remove();
                continue;
            }
            abilityLevelMap.put(ability.getName(), ability.getLevel());
            ability.setDisplayName(abi.getDisplayName());
            ability.setMatchId(matchId);
            ability.setMatchPlayerId(player.getId());
            ability.setSteamId(player.getSteamId());
            ability.setIndex(index++);
        }
        return saveBatch(list) ? list.size() : 0;
    }

    @Override
    public List<MatchPlayerAbility> findByMatchId(Integer matchId) {
        LambdaQueryWrapper<MatchPlayerAbility> query = Wrappers.lambdaQuery(MatchPlayerAbility.class)
                .eq(MatchPlayerAbility::getMatchId, matchId);
        return list(query);
    }

    private int deleteByMatchId(Integer matchId) {
        LambdaQueryWrapper<MatchPlayerAbility> query = Wrappers.lambdaQuery(MatchPlayerAbility.class).eq(MatchPlayerAbility::getMatchId, matchId);
        return baseMapper.delete(query);
    }

}
